//
//  CYDialTool.m
//  CreditAddressBook
//
//  Created by Lee on 15/5/27.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYDialTool.h"
#import "CYCallHistory.h"
#import "CYCallRecord.h"
#import "CYTool.h"
#import "CYABTool.h"
#import "CYDatabase.h"

@implementation CYDialTool

+ (void)initialize {
//    //获取沙盒中数据库的名称
//    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"by_call_history.sqlite"];
//    
//    //创建队列
//    _queue = [FMDatabaseQueue databaseQueueWithPath:path];
    
//    _queue = [[CYDatabase sharedDatabase] DBQueue];
    
    //建表并检查数据库版本
//    CYDBConfigLogic *config = [[CYDBConfigLogic alloc] init];
//    BOOL checkResult = [config checkDatabase:[LEDBTool sharedDatabase] newVersion:config.dbVersion];
//    if (!checkResult) {
//        CYLog(@"数据库版本检查失败");
//    }
}

/**
 *  随机ID
 *
 *  @return
 */
+ (NSString *)randomID {
    int a = arc4random();
    return [NSString stringWithFormat:@"%d", a];
}

/**
 *  固定范围内的随机数
 *
 *  @return
 */
+ (int)randomNumForm:(int)from to:(int)to {
    int a = to - from;
    if (a < 0) {
        return 0;
    }
    return arc4random() % a + from;
}

/**
 *  通话时长
 *
 *  @param time
 *
 *  @return 返回HH时mm分ss秒格式的时长
 */
+ (NSString *)callTime:(NSTimeInterval)time {
    int time1 = time;
    int hh = time/3600;
    int remainder = time1%3600;
    int mm = remainder/60;
    int ss = remainder%60;
    
    return [NSString stringWithFormat:@"%d时%d分%d秒", hh, mm, ss];
}

/**
  *  通话时长
  *
  *  @param time
  *
  *  @return 返回HH:mm:ss格式的时长
  */
+ (NSString *)callTime1:(NSTimeInterval)time {
    int time1 = time;
    int hh = time/3600;
    int remainder = time1%3600;
    int mm = remainder/60;
    int ss = remainder%60;
    
    if (hh > 0) {
        return [NSString stringWithFormat:@"%d:%d:%d", hh, mm, ss];
    }
    return [NSString stringWithFormat:@"%d:%d",mm, ss];
}

/**
 *  获取今天，本周，本月的时间
 *
 *  @param style
 *
 *  @return
 */
+ (NSNumber *)timeWithStyle:(NSInteger)style {
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];
    
    [components setHour:-[components hour]];
    [components setMinute:-[components minute]];
    [components setSecond:-[components second]];
    NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight);
    
    [components setHour:-24];
    [components setMinute:0];
    [components setSecond:0];
//    NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0];
    
    components = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[[NSDate alloc] init]];
    
    [components setDay:([components day] - ([components weekday] - 1))];
    NSDate *thisWeek  = [cal dateFromComponents:components];
    
    [components setDay:([components day] - 7)];
//    NSDate *lastWeek  = [cal dateFromComponents:components];
    
    [components setDay:([components day] - ([components day] -1))];
    NSDate *thisMonth = [cal dateFromComponents:components];
    
    [components setMonth:([components month] - 1)];
//    NSDate *lastMonth = [cal dateFromComponents:components];
    
//    NSLog(@"today=%@",today);
//    NSLog(@"yesterday=%@",yesterday);
//    NSLog(@"thisWeek=%@",thisWeek);
//    NSLog(@"lastWeek=%@",lastWeek);
//    NSLog(@"thisMonth=%@",thisMonth);
//    NSLog(@"lastMonth=%@",lastMonth);
    switch (style) {
        case CYTimeStyleDay:
            return [NSNumber numberWithDouble:today.timeIntervalSince1970];
            break;
            
        case CYTimeStyleWeek:
            return [NSNumber numberWithDouble:thisWeek.timeIntervalSince1970];
            break;
            
        case CYTimeStyleMonth:
            return [NSNumber numberWithDouble:thisMonth.timeIntervalSince1970];
            break;
            
        default:
            return @0;
            break;
    }
}

//将UIView转成UIImage
+ (UIImage *)imageFromView:(UIView *)theView {
    //UIGraphicsBeginImageContext(theView.bounds.size);iPhone4
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了
    UIGraphicsBeginImageContextWithOptions(theView.bounds.size, NO, [UIScreen mainScreen].scale);
    [theView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

//获得某个范围内的屏幕图像
+ (UIImage *)imageFromView: (UIView *) theView atFrame:(CGRect)r {
    UIGraphicsBeginImageContext(theView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    UIRectClip(r);
    [theView.layer renderInContext:context];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return  theImage;//[self getImageAreaFromImage:theImage atFrame:r];
}

+ (UIImage *)cutView:(UIView *)theView {
    CGRect rect = theView.frame;  //截取图片大小
    UIGraphicsBeginImageContext(rect.size);
    [theView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (NSString *)indexOfpinyin:(NSString *)py {
    NSString *index = @"";
    py = py.uppercaseString;
    for (int i = 0; i < py.length; i++) {
        int asc = [CYTool  characterToASCII:[py characterAtIndex:i]];
        if (asc >= 'A' && asc <= 'C') {
            index = [index stringByAppendingString:@"2"];
        } else if (asc >= 'D' && asc <= 'F') {
            index = [index stringByAppendingString:@"3"];
        } else if (asc >= 'G' && asc <= 'I') {
            index = [index stringByAppendingString:@"4"];
        } else if (asc >= 'J' && asc <= 'L') {
            index = [index stringByAppendingString:@"5"];
        } else if (asc >= 'M' && asc <= 'O') {
            index = [index stringByAppendingString:@"6"];
        } else if (asc >= 'P' && asc <= 'S') {
            index = [index stringByAppendingString:@"7"];
        } else if (asc >= 'T' && asc <= 'V') {
            index = [index stringByAppendingString:@"8"];
        } else if (asc >= 'W' && asc <= 'Z') {
            index = [index stringByAppendingString:@"9"];
        }
    }
    return index;
}

+ (NSString *)pinyinToASCII:(NSString *)py {
    return [CYTool stringToASCII:py.uppercaseString];
}
@end
